unit uActionOptions;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,
  uAction;

type
  TGameActionMenu = class(TForm)
    ActionType: TComboBox;
    TriggerArgs: TMemo;
    ArgumentCombo: TComboBox;
    DeleteButton: TButton;
    AddButton: TButton;
    OkayButton: TButton;
    CancelButton: TButton;
    ActionName: TEdit;
    procedure FormCanResize(Sender: TObject; var NewWidth, NewHeight: Integer;
      var Resize: Boolean);
    procedure FormShow(Sender: TObject);
    procedure ActionTypeChange(Sender: TObject);
    procedure AddButtonClick(Sender: TObject);
    procedure DeleteButtonClick(Sender: TObject);
    procedure OkayButtonClick(Sender: TObject);
    procedure CancelButtonClick(Sender: TObject);
    procedure ActionNameChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    act: TGameAction;
    actI: Integer;
  end;

var
  GameActionMenu: TGameActionMenu;

implementation

{$R *.dfm}

uses
  uMapEditor, uObjectOptions;

procedure TGameActionMenu.ActionNameChange(Sender: TObject);
begin
  act.actionType := self.ActionName.Text;
  self.Show;
end;

procedure TGameActionMenu.ActionTypeChange(Sender: TObject);
var
  i: integer;
begin
  for i := low(GameObjectOptions.obj.actions) to high(GameObjectOptions.obj.actions) do
    begin
      if (GameObjectOptions.obj.actions[i].actionType = self.ActionType.Text) then
        begin
          act := GameObjectOptions.obj.actions[i];
          actI := i;
          self.Show;
          exit;
        end;
    end;
end;

procedure TGameActionMenu.AddButtonClick(Sender: TObject);
var i: integer;
begin
  self.TriggerArgs.Lines.Add(self.ArgumentCombo.Text);
  act.arguments := '';
  for i := 0 to self.TriggerArgs.Lines.Count-1 do
    begin
      if (i > 0) then
        begin
          act.arguments := act.arguments + ',';
        end;
      act.arguments := act.arguments + self.TriggerArgs.Lines.Strings[i];
    end;
end;

procedure TGameActionMenu.CancelButtonClick(Sender: TObject);
begin
  self.Hide;
  GameObjectOptions.Show;
end;

procedure TGameActionMenu.DeleteButtonClick(Sender: TObject);
var i: integer;
begin
  self.TriggerArgs.Lines.Delete(self.TriggerArgs.Lines.Count-1);
  act.arguments := '';
  for i := 0 to self.TriggerArgs.Lines.Count-1 do
    begin
      if (i > 0) then
        begin
          act.arguments := act.arguments + ',';
        end;
      act.arguments := act.arguments + self.TriggerArgs.Lines.Strings[i];
    end;
end;

procedure TGameActionMenu.FormCanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
begin
  Resize := false;
end;

procedure TGameActionMenu.FormCreate(Sender: TObject);
begin
  self.DoubleBuffered := true;
  self.ArgumentCombo.Items.Add('OnEnter');
  self.ArgumentCombo.Items.Add('OnAttack');
  self.ArgumentCombo.Items.Add('OnDeath');
  self.ArgumentCombo.Items.Add('OnAlive');
  self.ArgumentCombo.Items.Add('OnBorn');
  self.ArgumentCombo.Items.Add('OnDebug');
  self.ArgumentCombo.Items.Add('OnGoOver');
end;

procedure TGameActionMenu.FormShow(Sender: TObject);
var i: integer; s: String;
begin
  self.ActionType.Items.Clear;
  for i := low(GameObjectOptions.obj.actions) to high(GameObjectOptions.obj.actions) do
    begin
      self.ActionType.Items.Add(GameObjectOptions.obj.actions[i].actionType);
    end;
  self.ActionType.Text := act.actionType;
  self.ActionName.Text := self.ActionType.Text;
  self.TriggerArgs.Lines.Clear;
  for i := 1 to length(act.arguments) do
    begin
      if (act.arguments[i] <> ',') then
        begin
          s := s + act.arguments[i];
        end;
      if ((act.arguments[i] = ',') or (i = length(act.arguments))) then
        begin
          self.TriggerArgs.Lines.Add(s);
          s := '';
        end;
    end;
end;

procedure TGameActionMenu.OkayButtonClick(Sender: TObject);
begin
  GameObjectOptions.obj.actions[actI] := act;
  self.Hide;
  GameObjectOptions.Show;
end;

end.
